//table标签权限ID
var tableDiv="tableClockVODiv";
//详情对话框div标签ID
var detailDiv="detailClockVODiv";
//增加和修改form标签ID
var formDiv = "addClockVOForm";
//增加和修改div标签ID
var addDlgDiv ="addClockVODiv";
//保存按钮
var addBtn = "ClockVOAddBtn";
//新增按钮  
var addDlgVO = "addClockBtn";
//模糊搜索form标签ID
var searchDiv= "searchClockVOForm";

//对象查询地址
var queryUrl="../system/clockVO/queryClockVOPageList";
//对象增加地址
var addUrl="../system/clockVO/saveClockVO"
//对象修改地址
var updateUrl="../system/clockVO/updateClockVOById"
//对象删除地址
var deleteUrl="../system/clockVO/deleteClockVOById"
//对象详情地址
var detailUrl="../system/clockVO/queryClockVOById"
//图片显示路径
var ftp = "http://172.16.1.52:3001/"

$(function(){

	//初始化时间插件
	$(".ui_timepicke").datepicker({
		  language:'zh-CN',
		  CustomFormat: "yyyy-mm-dd",
	      minView: "month", 
	      changeYear: true,
	      yearRange: "-200:+0",
	      timepicker:false,    
	      todayBtn: true,
	      autoclose:true
	});
	
	//显示提醒某行的详情
	$("#"+tableDiv+" tbody").on("click","#detailDataBtn",function(){
		var pk=$(this).attr("data-id");
		$.post(detailUrl,{clockId:pk},function(data){
			showDetailData(data);
		});
	});
	
	//显示详情对话框
	function showDetailData(data){
		for(var k in data){
			var ele =$("#"+detailDiv+" #"+k+"Detail");
			ele.html(data[k]);
		}
		$("#"+detailDiv).modal("show");
	};
	
	//删除
	$("#"+tableDiv+" tbody").on("click","#deleteDataBtn",function(){
		var pk=$(this).attr("data-id");
        $.post(detailUrl,{clockId:pk},function(clockData){
			var clockTime=clockData.clockTime;
			var clockId=clockData.clockId;
            confirm("您确定要删除此条信息吗 ?", function(isConfirm){
                if(isConfirm){
                    $.post(deleteUrl,{clockId:clockId,clockTime:clockTime},function(data){
                        if(data !=null && typeof(data)!="undefined"){
                            if(data.flg==true){
                                swal("成功","操作成功","success");
                            }else if(data.flg==false){
                                swal("失败","操作失败："+data.errorMsg,"error");
                            }
                        }
                        reloadData();
                    });

                }else{
                    swal("取消","您的操作已取消","error");
                }
            });
		});
	});
	
	var tabForm = $('#' + formDiv);
    var errorForm = $('.alert-error', tabForm);
    var successForm = $('.alert-success', tabForm);
	//初始化表单-新增和删除
	function initFormValidate(param){
		var url="";
		if(param=="add"){
			url=addUrl;
		}else if(param=="update"){
			url=updateUrl;
		}
		$("#"+formDiv).attr("action",url);
		
		//表单验证(新)
		$("#"+formDiv).validate({
	        errorElement: 'span', //default input error message container
	        errorClass: 'help-inline', // default input error message class
	        focusInvalid: false, // do not focus the last invalid input
	        ignore: "",
	        rules: {
	        		clockTime:{required:true,}, 
	        		clockMark:{required:true,}, 
	        		clockData:{required:true,}, 
	        		clockUrl:{required:true,}, 
	        		clockState:{required:true,}, 
	        		clockIntime:{} 
	        },
	        invalidHandler: function (event, validator) { //display error alert on form submit              
	            successForm.hide();
	            errorForm.show();
	            App.scrollTo(errorForm, -200);
	        },
	        highlight: function (element) { // hightlight error inputs
	            $(element).closest('.help-inline').removeClass('ok'); // display OK icon
	            $(element).closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
	        },
	        unhighlight: function (element) { // revert the change dony by hightlight
	            $(element).closest('.control-group').removeClass('error'); // set error class to the control group
	        },
	        success: function (label) {
	            label.addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
	            label.closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
	        },
	        messages:{},
	        submitHandler: function (form) {
	            successForm.show();
	            errorForm.hide();
	            valditFlag = true;//防止重复录入
	            //文件上传成功后，向后台插入数据
	            tabForm.ajaxSubmit({
					type:"post",
					url:tabForm.attr("action"),
					success:function(data){
						if(data.flg==true){
							swal("成功","操作成功", "success");
							$("#"+addDlgDiv).modal("hide");
							reloadData();
						}else if(data.flg==false){
							swal("失败","操作失败:"+data.errorMsg,"error");
							$("#"+addDlgDiv).modal("hide");
							reloadData();
						}
						
					}
				});
	            return false;
	        }
   		});
    	$('#bclockstateUpdate', '#'+formDiv).change(function () {
			$('#'+formDiv).validate().element($(this));
			$("br[name='br']",$(this).parent()).remove();
			var ele_str = '<br name="br"/>';
			$("#"+formDiv+" #bclockstateUpdate-error").css("font-size","14px");
			$("#"+formDiv+" #bclockstateUpdate").after(ele_str); 
		})
   		
	}
	
	
	//新增按钮
	$("#"+addDlgVO).bind("click",function(){
		reset();
		$("#"+addBtn).text("保存");
		$("#" + formDiv+ " #"+addBtn).attr("disabled",false);
		valditFlag =false;
		$("#"+ addDlgDiv).modal("show");
		initFormValidate("add");
	});
	
	//新增数据保存按钮
	$("#"+addBtn).on("click",function(){
		if(valditFlag){
			return;
		}
		$('#' + formDiv).submit();
		resetRequired();
	});
	
	//修改
	$("#"+tableDiv+" tbody").on("click","#updateDataBtn",function(){
		var pk=$(this).attr("data-id");
		$("."+formDiv+"_checktip").html(""); 
		reset();
		$("#"+addBtn).text("修改");
		$.ajax({url:detailUrl,data:{clockId:pk},type:"post",async:false,success:function(data){
			if(data !=null && typeof(data)!="undefined"){
				for(var k in data){
					var ele =$("#"+formDiv+" #"+k);
					ele.val(data[k]);
					if(k == "clockState"){
						$("#"+formDiv+" #bclockstateUpdate").selectpicker('val',data[k]);
						$("#"+formDiv+" #bclockstateUpdate").selectpicker('render');
						$("#"+formDiv+" #bclockstateUpdate").selectpicker('refresh');
					}
				}
				valditFlag = false;
				$("#"+ addDlgDiv).modal("show");
				initFormValidate("update");
			}
		}
		});
		initFormValidate("update");
	});
	
	//初始化表单
	function reset(){
		$("#"+formDiv).find("input").val("");
		$("#"+formDiv+" #bclockstateUpdate").selectpicker('val','');
		$("#"+formDiv+" #bclockstateUpdate").selectpicker('render');
		$("#"+formDiv+" #bclockstateUpdate").selectpicker('refresh');
		$(".help-inline").closest('.control-group').removeClass('ok');
   		$(".help-inline").closest('.control-group').removeClass('error');
   		$(".help-inline").closest('.control-group').removeClass('success');
   		$(".help-inline").removeClass('ok');
   	    $(".help-inline").html('');
   	    $("br[name='br']").remove();
   	    $(".alert-success").attr("display","none");
   	    $(".alert-error").attr("display","none");
   	    $(".alert-error").attr("style","");
   	    $(".alert-success").attr("style","");
   	    //$('#' + formDiv).reset();
	}
	
	//修改验证样式
	function resetRequired(){
		$("br[name='br']",$(this).parent()).remove();
		var ele_str = '<br name="br"/>';
		$("#"+formDiv+" #bclockstateUpdate-error").css("font-size","14px");
		$("#"+formDiv+" #bclockstateUpdate").after(ele_str); 
	}

	function reloadResponsNum(){
        $.post("../system/clockVO/queryTaskNum",function (data) {
            var num1 = data.manngernum;
            var num2 = data.responsenum;
            $("#mangernumSpan").html("wait:" + num1);
            $("#responseSpan").html("fail:" + num2);

        })
	}

    reloadResponsNum();
    setInterval(reloadResponsNum,5000);
});
